package experiment.e09.code;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/12/1 14:29
 * @package experiment.e09.code
 * @description 文件说明
 */

public class E02 {
}

class Test {
    public static void main(String arg[]) {
        Ticket.setNum(20);
        Ticket ticket1 = new Ticket();
        Ticket ticket2 = new Ticket();
        Ticket ticket3 = new Ticket();

        ticket1.setName("销售点1");
        ticket2.setName("销售点2");
        ticket3.setName("销售点3");

        ticket1.start();
        ticket2.start();
        ticket3.start();

    }
}

class Ticket extends Thread {
    static int num;

    public static void setNum(int num) {
        Ticket.num = num;
    }

    public void run() {
        synchronized (this) {
            while (true) {
                try {
                    Thread.sleep((long) (Math.random() * 1000));
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (num > 0)
                    System.out.println(Thread.currentThread().getName() + "正在售第" + num-- + "张票");
                if (num == 0) break;
            }
        }

    }
}

